/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpaintball;

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;

/**
 *
 * @author ondra
 */
public class GameWindow extends JFrame {

    Canvas canvas;
    GameMenu menu;
    Game     game;
    LocalClient client;

    GameWindow() {
        // setup window
        super("JPaintball");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new BorderLayout());
        setFocusable(true);

        // add menu
        menu = new GameMenu(this);
        setJMenuBar(menu);

        // add canvas to the center
        canvas = new Canvas();
        add(canvas, BorderLayout.CENTER);

        // auto set window size
        pack();

        // center window
        setLocationRelativeTo(null);

        setVisible(true);
    }

    public void showStartServerDialog() {
        StartServerDialog d = new StartServerDialog(this);
    }
    public void showJoinDialog() {
        JoinDialog d = new JoinDialog(this);
    }
    public void startGame() {

        this.addKeyListener(new GameKeyboardListener());
        this.addMouseListener(new GameMouseListener());
        this.addMouseMotionListener(new GameMouseMotionListener());

        game = new Game();
        client = new LocalClient(game,canvas);
        new NetworkServer(2222).start();

    }
    public void joinGame() {
        // nothing
    }
    public void stopServer() {
        // nothing
    }
    ///////////////////////////////////////////////////////////////////
    //  CONTROL LISTENERS
    ///////////////////////////////////////////////////////////////////

    private class GameKeyboardListener extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
           client.setKeys(e);
        }
        public void keyReleased(KeyEvent e) {
            client.resetKeys(e);
        }

    }
     private class GameMouseMotionListener extends MouseMotionAdapter {

        @Override
        public void mouseMoved(MouseEvent e) {
            client.setMousePosition(e.getX(),e.getY());
        }
        @Override
        public void mouseDragged(MouseEvent e) {
            client.setMousePosition(e.getX(),e.getY());
        }

    }
    private class GameMouseListener extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            if(e.getButton() == MouseEvent.BUTTON1) {
                client.shoot();
            }
        }

    }


}
